package control;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import swimv2.ejb.entity.Capacita;
import swimv2.ejb.entity.CapacitaRichiesta;
import swimv2.ejb.remote.GestioneCapacitaRemote;
import swimv2.ejb.util.LoginToken;
import util.HtmlUtil;

/**
 * Servlet implementation class RichiestaCapacita
 */
public class RichiestaCapacita extends Servlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see Servlet#Servlet()
     */
    public RichiestaCapacita() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		LoginToken tok = (LoginToken) request.getSession().getAttribute("LoginToken");
		String username=tok.getUsername();
		try {
        	Context jndiContext;
			jndiContext = new javax.naming.InitialContext();
			Object ref1 = jndiContext.lookup("GestioneCapacita/remote");
			GestioneCapacitaRemote gestioneCapacitaRemote =(GestioneCapacitaRemote) PortableRemoteObject.narrow(ref1, GestioneCapacitaRemote.class);
			List<CapacitaRichiesta> elencoRichieste = gestioneCapacitaRemote.elencoRichiesteCapacita(username);
			request.setAttribute("elencoRichieste", elencoRichieste);
			//HtmlUtil.redirect(request, response, "/RichiestaCapacita");
		
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		HtmlUtil.CompletaEdIncludi(request, response, "/WEB-INF/pages/richiestaCapacita.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		LoginToken tok = (LoginToken) request.getSession().getAttribute("LoginToken");
		String username=tok.getUsername();
		try {
			String capacita = request.getParameter("capacita");
        	Context jndiContext;
			jndiContext = new javax.naming.InitialContext();
			Object ref1 = jndiContext.lookup("GestioneCapacita/remote");
			GestioneCapacitaRemote gestioneCapacitaRemote =(GestioneCapacitaRemote) PortableRemoteObject.narrow(ref1, GestioneCapacitaRemote.class);
			gestioneCapacitaRemote.richiediCapacita(username, capacita);
			
			HtmlUtil.redirect(request, response, "/RichiestaCapacita");
		
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	
	}

}
